<template>
    <div class="customer-view-customer">
        <div class="page-head">
            <el-input v-model="searchValue" prefix-icon="el-icon-search" placeholder="请输入客户号/客户姓名/手机号"></el-input>
        </div>
        <div class="page-tabs">
            <div :class="{ is_active: e == activeTab }" class="page-tabs-item" v-for="e in 2" :key="e" @click="handleChangeList(e)">
                {{ e | indexFilter(that) }}
            </div>
        </div>
        <div v-if="customerList.length === 0" class="page-no-content" v-loading="loading">
            <img src="@/assets/img/no-words.png" alt="" />
            <p>暂无内容</p>
        </div>
        <div v-else class="page-content" v-loading="loading">
            <div class="page-content-item" v-for="(item, index) in customerList" :key="index" @click="handleView(item)">
                <div class="page-content-item-left">
                    <img :src="item.avatar || $defaultAvatar" alt="" />
                    <div>
                        <p>{{ item.name }}</p>
                        <span v-if="item.assetsLevel">{{ item.assetsLevel }}</span>
                    </div>
                </div>
                <div class="page-content-item-right" v-if="activeTab == 2">
                    <p>{{ item.mobile | mobileFilter }}</p>
                    <div class="btn">复制手机号</div>
                </div>
            </div>
            <div class="page-content-btn" v-if="showBtn" @click="handleGetMore">查看更多</div>
        </div>
    </div>
</template>
<script>
    import managerViewApi from '@/api/manager-view';
    export default {
        data() {
            return {
                showBtn: false,
                customerList: [],
                listInfo: {
                    list1: 0,
                    list2: 0
                },
                page: {
                    current: 1,
                    size: 10,
                    total: 0
                },
                activeTab: 1,
                searchValue: '',
                that: this,
                loading: false,
                defaultAvatar: ''
            };
        },
        filters: {
            indexFilter(index, that) {
                let tabs = ['我的客户', '潜在客户'],
                    keys = ['list1', 'list2'];
                return tabs[index - 1] + '(' + that.listInfo[keys[index - 1]] + ')';
            },
            mobileFilter(val) {
                return val.slice(0, 3) + '****' + val.slice(7);
            }
        },
        watch: {
            searchValue: {
                handler(val) {
                    this.customerList = [];
                    this.getList();
                }
            }
        },
        created() {
            this.initList();
        },
        methods: {
            initList() {
                let obj = {
                        current: this.page.current,
                        name: this.searchValue,
                        size: this.page.size
                    },
                    wxUserId = this.$store.state.vuex_wxUserId,
                    num = 0;
                this.loading = true;
                managerViewApi
                    .getMyCustomerList(obj, wxUserId)
                    .then((res) => {
                        if (res.data.code != 0) return;
                        this.customerList.push(...res.data.data.records);
                        this.page.total = res.data.data.total;
                        this.listInfo.list1 = res.data.data.total;
                        this.showBtn = this.page.total > this.page.current * this.page.size;
                    })
                    .finally(() => {
                        num++;
                        this.check(num);
                    });
                managerViewApi
                    .getDisCustomerList(obj, wxUserId)
                    .then((res) => {
                        if (res.data.code != 0) return;
                        this.listInfo.list2 = res.data.data.total;
                    })
                    .finally(() => {
                        num++;
                        this.check(num);
                    });
            },
            check(data) {
                if (data != 2) return;
                this.loading = false;
            },
            handleGetMore() {
                // 加载更多数据
                this.page.current += 1;
                this.getList(1);
            },
            getList(data) {
                // 判断条件
                if (!data) {
                    this.page.current = 1;
                }
                let obj = {
                        current: this.page.current,
                        name: this.searchValue,
                        size: this.page.size
                    },
                    wxUserId = this.$store.state.vuex_wxUserId;
                this.loading = true;
                if (this.activeTab == 1) {
                    managerViewApi
                        .getMyCustomerList(obj, wxUserId)
                        .then((res) => {
                            if (res.data.code != 0) return;
                            this.customerList.push(...res.data.data.records);
                            this.page.total = res.data.data.total;
                            this.listInfo.list1 = res.data.data.total;
                            this.showBtn = this.page.total > this.page.current * this.page.size;
                        })
                        .finally(() => {
                            this.loading = false;
                        });
                } else {
                    managerViewApi
                        .getDisCustomerList(obj, wxUserId)
                        .then((res) => {
                            if (res.data.code != 0) return;
                            this.customerList.push(...res.data.data.records);
                            this.page.total = res.data.data.total;
                            this.listInfo.list2 = res.data.data.total;
                            this.showBtn = this.page.total > this.page.current * this.page.size;
                        })
                        .finally(() => {
                            this.loading = false;
                        });
                }
            },
            handleChangeList(index) {
                if (this.activeTab == index) return;
                this.activeTab = index;
                this.customerList = [];
                this.getList();
            },
            handleView(data) {
                if (this.activeTab == 1) {
                    this.$router.push({
                        path: '/customerDetail',
                        query: {
                            wxUserId: this.$store.state.vuex_wxUserId,
                            wxExternalUserid: data.wxExternalUserid
                        }
                    });
                } else {
                    let copyText = data.mobile;
                    this.$copyText(copyText).then(
                        (e) => {
                            console.log('复制成功:', e);
                            this.$wxApi.navigateToAddCustomer();
                        },
                        (e) => {
                            this.$toast({ text: '复制失败', type: 'error' });
                            console.log('复制失败:', e);
                        }
                    );
                }
            }
        }
    };
</script>
<style scoped lang="scss">
    .customer-view-customer {
        background: #ffffff;
        min-height: calc(100vh - 61px);
        .page-head {
            padding: 8px 16px;
            box-sizing: border-box;
            /deep/ .el-input__inner {
                background: #f0f0f0;
                border-radius: 14px;
                width: 100%;
                height: 36px;
                line-height: 36px;
                border: none;
            }
        }
        .page-tabs {
            padding: 0 16px;
            box-sizing: border-box;
            height: 45px;
            display: flex;
            &-item {
                flex: 1;
                text-align: center;
                line-height: 45px;
                font-size: 14px;
                font-weight: 500;
                color: rgba(44, 44, 44, 0.5);
            }
            .is_active {
                position: relative;
                color: #2c2c2c;
                font-weight: 500;
            }
            .is_active:after {
                content: '';
                width: 24px;
                height: 4px;
                background: #ed5555;
                border-radius: 2px;
                position: absolute;
                left: 0;
                right: 0;
                bottom: 0;
                margin: 0 auto;
            }
        }
        .page-no-content {
            text-align: center;
            font-size: 15px;
            color: #696969;
            img {
                width: 140px;
                height: 84px;
                margin: 10px 0 15px 0;
            }
        }
        .page-content {
            padding-bottom: 20px;
            box-sizing: border-box;
            &-item {
                height: 73px;
                padding: 16px;
                box-sizing: border-box;
                display: flex;
                position: relative;
                justify-content: space-between;
                img {
                    border-radius: 50%;
                    width: 40px;
                    height: 40px;
                    margin-right: 8px;
                }
                p {
                    font-size: 14px;
                    font-weight: 500;
                    color: #202020;
                    margin-bottom: 5px;
                }
                span {
                    display: inline-block;
                    font-size: 10px;
                    font-weight: 500;
                    color: #ffffff;
                    border-radius: 4px;
                    background: #d5a874;
                    padding: 1px 4px;
                }
                &-left {
                    display: flex;
                }
                &-right {
                    font-size: 16px;
                    font-weight: 500;
                    color: #202020;
                    p {
                        text-align: center;
                    }
                    .btn {
                        border-radius: 13px;
                        border: 1px solid #ed5550;
                        padding: 2px 2px;
                        font-size: 10px;
                        text-align: center;
                    }
                }
            }
            &-item::after {
                content: '';
                width: calc(100% - 16px);
                height: 1px;
                background: #fafafa;
                position: absolute;
                bottom: 0;
                left: 16px;
            }
            &-btn {
                font-size: 14px;
                height: 40px;
                width: 80%;
                margin: 20px auto;
                text-align: center;
                font-weight: 400;
                color: rgba(44, 44, 44, 0.8);
                line-height: 40px;
                border-radius: 20px;
                border: 1px solid rgba(44, 44, 44, 0.1);
            }
        }
    }
</style>
